package com.handler;

import com.yly.BetBean;
import com.yly.BetDao;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by tj on 2016/10/28.
 */
@Repository
public class BetService {

    public final static String WEB_URL = "http://kj.13322.com/lhc_history_d";

    @Autowired
    public BetDao betDao;

    private List<String> generateList() {
        List<String> list = new ArrayList<>();
        for (int i = 2012; i <= Calendar.getInstance().get(Calendar.YEAR); i++) {
            list.add(WEB_URL + i + "_0.html");
        }
        return list;
    }

//    /**
//     * 用来最初制作数据库的
//     */
//    public void parse() {
//        List<String> list = generateList();
//        for (String s : list) {
//            Document doc = null;
//            try {
//                doc = Jsoup.connect(s).get();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            Element table = doc.getElementById("trend_table");
//            Elements trs = table.getElementsByTag("tr");
//            for (int i = 0; i < trs.size(); i++) {
//                if (i < 2) {
//                    continue;
//                }
//                Element tr = trs.get(i);
//                BetBean betBean = new BetBean();
//
//                Elements tds = tr.getElementsByTag("td");
//                betBean.setId(Integer.parseInt(tds.get(0).text())); //期数
//
//                try {
//                    SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);//日期
//                    Date date = sdf1.parse(tds.get(1).text());
//                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                    String sDate = sdf.format(date);
//                    betBean.setDate(sDate);
//                } catch (ParseException e) {
//                    e.printStackTrace();
//                }
//
//                Elements mazais = tds.get(2).getElementsByAttributeValue("name", "markSixCodeNumber"); //马仔
//                betBean.setM1(Integer.parseInt(mazais.get(0).child(0).text()));
//                betBean.setM2(Integer.parseInt(mazais.get(1).child(0).text()));
//                betBean.setM3(Integer.parseInt(mazais.get(2).child(0).text()));
//                betBean.setM4(Integer.parseInt(mazais.get(3).child(0).text()));
//                betBean.setM5(Integer.parseInt(mazais.get(4).child(0).text()));
//                betBean.setM6(Integer.parseInt(mazais.get(5).child(0).text()));
//                betBean.setTe(Integer.parseInt(tds.get(3).child(0).child(0).text()));
//
//                int zfdx = tds.get(5).text().contains("big") ? 0 : 1;
//                betBean.setZf_daxiao(zfdx);
//                int zfdansh = tds.get(6).text().contains("odd") ? 0 : 1;
//                betBean.setZf_danshuang(zfdansh);
//                betBean.setTe_shengxiao(tds.get(7).text().split("\\.")[2]);
//                betBean.setTe_jiaye(tds.get(8).text().split("\\.")[2]);
//                int tedx = tds.get(9).text().contains("single") ? 0 : 1;
//                betBean.setTe_danshuang(tedx);
//                int tedaxiao = tds.get(10).text().contains("bignum") ? 0 : 1;
//                betBean.setTe_daxiao(tedaxiao);
//                betBean.setTe_color(tds.get(11).text().split("\\.")[2]);
//                int weishudaxiao = Integer.parseInt(tds.get(14).text()) > 4 ? 0 : 1;
//                betBean.setTew_daxiao(weishudaxiao);
//                betDao.addBetBean(betBean);
//            }
//        }
//    }

    @Scheduled(cron = "0 0 19 * * ? ")
    private void testSchedule() {
        int yearNow = Calendar.getInstance().get(Calendar.YEAR);
        String url = WEB_URL + yearNow + "_0.html";
        storePerIssue(url);
    }

    private void storePerIssue(String url) {
        Document doc = null;
        try {
            doc = Jsoup.connect(url).timeout(20000).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Element table = doc.getElementById("trend_table");
        Elements trs = table.getElementsByTag("tr");

        Element tr = trs.get(2);
        BetBean betBean = new BetBean();

        Elements tds = tr.getElementsByTag("td");

        int newestId = Integer.parseInt(tds.get(0).text()); //期数
        if (newestId == betDao.getMaxId()) {
            System.out.println("没有更新");
            return;
        }
        betBean.setId(newestId);

        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);//日期
            Date date = sdf1.parse(tds.get(1).text());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String sDate = sdf.format(date);
            betBean.setDate(sDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        Elements mazais = tds.get(2).getElementsByAttributeValue("name", "markSixCodeNumber"); //马仔
        betBean.setM1(Integer.parseInt(mazais.get(0).child(0).text()));
        betBean.setM2(Integer.parseInt(mazais.get(1).child(0).text()));
        betBean.setM3(Integer.parseInt(mazais.get(2).child(0).text()));
        betBean.setM4(Integer.parseInt(mazais.get(3).child(0).text()));
        betBean.setM5(Integer.parseInt(mazais.get(4).child(0).text()));
        betBean.setM6(Integer.parseInt(mazais.get(5).child(0).text()));
        betBean.setTe(Integer.parseInt(tds.get(3).child(0).child(0).text()));

        int zfdx = tds.get(5).text().contains("big") ? 0 : 1;
        betBean.setZf_daxiao(zfdx);
        int zfdansh = tds.get(6).text().contains("odd") ? 0 : 1;
        betBean.setZf_danshuang(zfdansh);
        betBean.setTe_shengxiao(tds.get(7).text().split("\\.")[2]);
        betBean.setTe_jiaye(tds.get(8).text().split("\\.")[2]);
        int tedx = tds.get(9).text().contains("single") ? 0 : 1;
        betBean.setTe_danshuang(tedx);
        int tedaxiao = tds.get(10).text().contains("bignum") ? 0 : 1;
        betBean.setTe_daxiao(tedaxiao);
        betBean.setTe_color(tds.get(11).text().split("\\.")[2]);
        int weishudaxiao = Integer.parseInt(tds.get(14).text()) > 4 ? 0 : 1;
        betBean.setTew_daxiao(weishudaxiao);

        betDao.addBetBean(betBean);
        System.out.println(betBean.getId() + "期数据更新完毕");
    }

    public void calcHistory() {
        List<BetBean> beans = betDao.getAllBetBeans();
        for (BetBean bean : beans) {
            calJiaye(bean);
        }
        System.out.println("jiayeMax = " + jiayeMax + "; jiayeType = " + jiayeType + "; jiaye_id = " + jiaye_id);
    }

    private int jiayeSerialCount= 1;
    private String jiayeType = "";
    private int jiaye_id;
    private int jiayeMax;
    private void calJiaye(BetBean bean) {
        if (jiayeType.equals(bean.getTe_jiaye())) {
            jiayeSerialCount ++;
            if (jiayeSerialCount > jiayeMax) {
                jiaye_id = bean.getId();
                jiayeMax = jiayeSerialCount;
            }
        } else {
            jiayeType = bean.getTe_jiaye();
            jiayeSerialCount = 1;
        }
    }

    public static void main(String[] args) {
//        new BetService().parse();
//        String result = new BetService().crawl();
//        System.out.println(result);
        Calendar.getInstance().get(Calendar.YEAR);
    }

}
